<?php
class Login extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
            $this->load->model('user_m');
        }
        
        function checklogin($uname,$upass){
            
            $user=$this->user_m->select(array('name'=>$this->input->post('uname'),'pass'=>$this->input->post('upass')));
            //$user=$this->user_m->select(array('name'=>$uname,'pass'=>$upass));
            if($user){
                $this->load->library('session');
                $data=array('uid'=>$user[0]->id,'uname'=>$user[0]->name);
                $this->session->set_userdata($data);
                $res=array('flag'=>'success','name'=>$data['uname'],'userid'=>$data['uid'],'sessionid'=> $this->session->userdata('session_id'));
                echo json_encode($res);
            }else{
                $res=array('flag'=>'error','name'=>'','userid'=>'','sessionid'=>'');
                echo json_encode($res);
            }
        }
    
	function index(){
            //$this->load->library('form_validation');
            
            $this->form_validation->set_rules('uname','Name','required');
            $this->form_validation->set_rules('upass','Pass','required');
            
            if($this->form_validation->run()){
                $user=$this->user_m->select_user($this->input->post('uname'));
                if($user){
                    $data=array('uid'=>$user[0]->id,'uname'=>$user[0]->name,'upass'=>$user[0]->pass);
                    //var_dump($user);
                    if($data['upass']==$this->input->post('upass')){
                        $this->load->library('session');
                        $arr=array('uid'=>$data['uid']);
                        $this->session->set_userdata($arr);
                        //echo $data['uid'].'-'.$this->session->userdata['uid'];
                        redirect('news');
                        //$this->load->view('login_success',$data);
                    }else{
                        $err=array('nameerr'=>'','passerr'=>'pass error');
                        $this->load->view('login',$err);
                    } 
                }else{
                    $err=array('nameerr'=>'name error','passerr'=>'');
                    $this->load->view('login',$err);
                }
            }else{
                $err=array('nameerr'=>'','passerr'=>'');
                $this->load->view('login',$err);
            }            
	}

}
?>